//
//  JFBMerchandiseCategoryDS.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/16.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBMerchandiseCategoryDS.h"

@interface JFBMerchandiseCategoryDS()
@property (nonatomic, strong) NSMutableArray * items;
@end

@implementation JFBMerchandiseCategoryDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    /*
     //商品唯一的ID
     @property (nonatomic, assign) NSUInteger ID;
     // 商品名
     @property (nonatomic, strong) NSString *categoryName;
     // 分类下商品总数
     @property (nonatomic, assign) NSUInteger total;
     // 分类下上架商品数
     @property (nonatomic, assign) NSUInteger count;
     //上一级分类ID
     @property (nonatomic, assign) NSUInteger category2_ID;

     */
    self.categoryName = [[attributes objectForKey: @"categoryName"] isEqual: [NSNull null]] ? 0 : [attributes objectForKey: @"categoryName"];
    self.categoryId = [[attributes objectForKey: @"categoryId"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"categoryId"]  integerValue];
    self.total = [[attributes objectForKey: @"total"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"total"]  integerValue];
    self.shelvesNum = [[attributes objectForKey: @"shelvesNum"] isEqual: [NSNull null]] ? 0 : [[attributes objectForKey: @"shelvesNum"]  integerValue];
    self.categoryTreeNodeId = [[attributes objectForKey:@"categoryTreeNodeId"] isEqual:[NSNull null]]? 0 :[[attributes objectForKey:@"categoryTreeNodeId"] integerValue];
    self.pullOffShelvesNum = [[attributes objectForKey:@"pullOffShelvesNum"] isEqual:[NSNull null]]?0:[[attributes objectForKey:@"pullOffShelvesNum"] integerValue];
    return self;
}

//加载商品分类列表
+ (NSURLSessionDataTask *)loadMerchandiseCategoryList:(void (^)(NSArray *categoryList, NSError *error))block pager:(JFBPager*)pager {
    
    
    NSDictionary *dic = @{@"categoryTreeNodeId":[[NSUserDefaults standardUserDefaults]objectForKey:@"categoryTreeNodeId"],@"ut":[[NSUserDefaults standardUserDefaults] objectForKey:@"ut"]};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/back-product/queryMerchantCategoryTreeLevelNode.do" parameters:dic success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"code"];
        if( [error isEqual: @"0"]) {
            NSMutableArray *result = [@[] mutableCopy];
            id jsonData = [JSON valueForKeyPath:@"data"];
            NSArray *response = [jsonData valueForKeyPath:@"categoryList"];
            for( NSDictionary *staff in response ) {
                [result addObject:[[JFBMerchandiseCategoryDS alloc] initWithAttributes:staff]];
            }
            NSLog(@"json:%@", JSON);
            
            NSLog(@"%@", result);
            
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
}
@end
